package pages;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import utils.ReportWriter;

public class SearchPage extends BasePage {

    private static final String PAGE_URL = "search/";
    private static final By buyButton = By.name("topurchasesfromcatalog");


    public SearchPage(WebDriver driver)
    {
        super(driver,PAGE_URL);
    }


    public void open()
    {
        driver.get(pageUrl);

    }

    public void clickBuyBtn() throws InterruptedException {
        if ( driver.findElements(buyButton).size() > 0){
            driver.findElement(buyButton).click();
        }
        else if ( driver.findElements(buyButton).size()==0) {
            driver.findElement(By.className("ibutton-css-link-blue")).click();
        }

        try {
            Thread.sleep(15000);
            ReportWriter.info("Waiting for a cart loading");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }
    }


}
